package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.activity.order.OrderDetailActivity;
import com.qeeyou.apps.android.craftsman.po.CommentInfo;
import com.qeeyou.apps.android.craftsman.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class CommentAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<CommentInfo> commentList;
    private LayoutInflater mLayoutInflater;

    public CommentAdapter(Activity activity, List<CommentInfo> commentList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if(this.commentList == null) {
            this.commentList = new ArrayList<CommentInfo>();
        }
        this.commentList.clear();
        if(commentList != null && commentList.size() > 0) {
            this.commentList.addAll(commentList);
        }
    }

    public void setItems(List<CommentInfo> commentList) {
        if(this.commentList == null) {
            this.commentList = new ArrayList<CommentInfo>();
        }
        this.commentList.clear();
        if(commentList != null && commentList.size() > 0) {
            this.commentList.addAll(commentList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.commentList != null ? this.commentList.size() : 0;
        return count;
    }

    @Override
    public CommentInfo getItem(int i) {
        if(i < 0 || i >= this.commentList.size()) {
            return null;
        }
        return this.commentList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if(view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_comment, null, false);
            holder = new ViewHolder();
            holder.imgvAvatar = (ImageView) view.findViewById(R.id.imgvAvatar4ItemComment);
            holder.txtvName = (TextView) view.findViewById(R.id.txtvName4ItemComment);
            holder.ratingBar = (RatingBar) view.findViewById(R.id.ratingBar4ItemComment);
            holder.txtvTime = (TextView) view.findViewById(R.id.txtvTime4ItemComment);
            holder.txtvComment = (TextView) view.findViewById(R.id.txtvComment4ItemComment);
            holder.imgvPic1 = (ImageView) view.findViewById(R.id.imgvComment14ItemComment);
            holder.imgvPic2 = (ImageView) view.findViewById(R.id.imgvComment24ItemComment);
            holder.imgvPic3 = (ImageView) view.findViewById(R.id.imgvComment34ItemComment);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        CommentInfo commentInfo = this.commentList.get(position);
        if(commentInfo != null) {
            holder.txtvName.setText(commentInfo.getName());
            holder.txtvTime.setText(commentInfo.getServicetime());
            holder.txtvComment.setText(commentInfo.getCommentmemo());
            if(commentInfo.getUserAvatar() != null) {
                String avatarStr = commentInfo.getUserAvatar();
                avatarStr = avatarStr.replace(";", "");
                String imageUrl = ImageLoader.getInstance().getImageUrl(avatarStr);
                ImageLoader.getInstance().displayHeaderPic(mActivity, imageUrl, holder.imgvAvatar);
            } else {
                ImageLoader.getInstance().displayImageById(mActivity, R.drawable.ic_master_avatar, holder.imgvAvatar);
            }
            if(commentInfo.getSerquality() != null && !commentInfo.getSerquality().trim().equalsIgnoreCase("")) {
                holder.ratingBar.setRating(Float.parseFloat(commentInfo.getSerquality()));
            } else {
                holder.ratingBar.setRating(Float.parseFloat("0"));
            }
            if(commentInfo.getCommentimg() != null && !commentInfo.getCommentimg().trim().equalsIgnoreCase("")) {
                String[] imageUrls = commentInfo.getCommentimg().split("\\;");
                if(imageUrls != null && imageUrls.length > 0) {
                    if(imageUrls.length >= 3) {
                        holder.imgvPic1.setVisibility(View.VISIBLE);
                        holder.imgvPic2.setVisibility(View.VISIBLE);
                        holder.imgvPic3.setVisibility(View.VISIBLE);
                        String imageUrl1 = ImageLoader.getInstance().getImageUrl(imageUrls[0]);
                        String imageUrl2 = ImageLoader.getInstance().getImageUrl(imageUrls[1]);
                        String imageUrl3 = ImageLoader.getInstance().getImageUrl(imageUrls[2]);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl1, holder.imgvPic1);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl2, holder.imgvPic2);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl3, holder.imgvPic3);
                    } else if(imageUrls.length == 2) {
                        holder.imgvPic1.setVisibility(View.VISIBLE);
                        holder.imgvPic2.setVisibility(View.VISIBLE);
                        holder.imgvPic3.setVisibility(View.INVISIBLE);
                        String imageUrl1 = ImageLoader.getInstance().getImageUrl(imageUrls[0]);
                        String imageUrl2 = ImageLoader.getInstance().getImageUrl(imageUrls[1]);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl1, holder.imgvPic1);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl2, holder.imgvPic2);
                    } else {
                        holder.imgvPic1.setVisibility(View.VISIBLE);
                        holder.imgvPic2.setVisibility(View.INVISIBLE);
                        holder.imgvPic3.setVisibility(View.INVISIBLE);
                        String imageUrl1 = ImageLoader.getInstance().getImageUrl(imageUrls[0]);
                        ImageLoader.getInstance().displayImage(mActivity, imageUrl1, holder.imgvPic1);
                    }
                } else {
                    holder.imgvPic1.setVisibility(View.GONE);
                    holder.imgvPic2.setVisibility(View.GONE);
                    holder.imgvPic3.setVisibility(View.GONE);
                }
            } else {
                holder.imgvPic1.setVisibility(View.GONE);
                holder.imgvPic2.setVisibility(View.GONE);
                holder.imgvPic3.setVisibility(View.GONE);
            }
        }
        return view;
    }

    static class ViewHolder {
        ImageView imgvAvatar;
        TextView txtvName;
        RatingBar ratingBar;
        TextView txtvTime;
        TextView txtvComment;
        ImageView imgvPic1;
        ImageView imgvPic2;
        ImageView imgvPic3;
    }
}
